** Data reading and variable selection from raw data
** Spain, "Occupational Prestige and Social Structure" (Prestigio Ocupacional Y Estructura Social)


** 01. Reading data **

cap log close
clear all
set more off
cd /*insert you work directory here*/

#delimit ;
infix
nsibs 1320-1321  // q83
birthorder 1322-1323  // q84
faeduc 1316-1317  // q82-01
maeduc 1318-1319  // q82-02
sex 1380  // q 101
age 1381-1382  // q 102
educ 1383-1384 // q 103
occ 1035-1037  // question 24
firstocc 944-946 // q 7

using /*read your data here*/  

lab def paeduc 
1 "illiterate (no sabia leer ni escribir)"
2 "< primary (menos de estudios primarios)"
3 "=primary (educacion primaria)"
4 "initial voc qual (cualificacion profesional grado inicial: FP grado inicial)"
5 "lower sec (educacion secundaria: primera etapa)"
6 "middle grade FP (FP de grado medio)"
7 "upper sec (bachillerato-educacion secundaria: 2a etapa)"
8 "upper grade FP (FP de grado superior)"
9 "technical engineering (ingenieria tecnica)"
10 "diploma (diplomatura)"
11 "grade (grado)"
12 "bachelor deg (licenciatura)"
13 "architecture/engineering (arquitectura/ingenieria superior)"
14 "master deg(master oficial universitario)"
15 "other postgrad:unofficial(otros estudios de posgrado: no oficiales)"
16 "other postgrad:official(otros estudios de posgrado oficiales)"
17 "doctorate(doctorado)"
18 "others(otros)"
97 "no parent(no procede)"
98 "don't remember(no recuerda)"
99 "NC";
lab val maeduc faeduc paeduc;

lab def sex 1 "male" 2 "female";
lab val sex sex;

lab def educ 
0 "still student"
1 "< primary (menos de estudios primarios)"
2 "= primary (educacion primaria)"
3 "initial voc qual (cualificacion profesional grado inicial: FP grado inicial)"
4 "lower sec (educacion secundaria: primera etapa)"
5 "middle grade FP (FP de grado medio)"
6 "upper sec (bachillerato-educacion secundaria: 2a etapa)"
7 "upper grade FP (FP de grado superior)"
8 "technical engineering (ingenieria tecnica)"
9 "diploma (diplomatura)"
10 "grade (grado)"
11 "bachelor deg (licenciatura)"
12 "architecture/engineering (arquitectura/ingenieria superior)"
13 "master deg (master oficial universitario)"
14 "other postgrad: unofficial (otros estudios de posgrado: no oficiales)"
15 "other postgrad: oficiales (otros estudios de posgrado oficiales)"
16 "doctorate (doctorado)"
17 "others (otros)"
98 "NS"
99 "NC";
lab val educ educ;
#delimit cr

recode maeduc faeduc educ nsibs birthorder (97/99=.)
recode firstocc occ (0 999=.)


** 02. Consructing year and country variables **

ge year=2013
lab var year "survey year"

ge country=724
lab var country "ISO country code"
// Spain: 724 (ISO Country Codes) 


** 03. ID variables **

ge pid=_n
lab var pid "person id"


** 04. Basic Demographics (Sex and Age/birth year) **

ge birthyr=2013-age
lab var birthyr "year of birth"
lab var age "age"
lab var sex "sex"


** 05. Siblings **
lab var nsibs "number of siblings"
lab var birthorder "birth order"


** 06. Own education **
lab var educ "own education"


** 07. Parents' education: Father and/or Mother **
lab var faeduc "father's education"
lab var maeduc "mother's education"


** 08. Own occupation **
lab var occ "own occupation (recent/current)"
lab var firstocc "own first occupation"


** 09. Parents' occupation: not available **


** 10. Tabulate the Identified Variables **

log using  /*insert you work directory here*/, replace text


** Sex **
tab sex

** Age, Birth Year **
sum age birthyr, d

** Siblings **
sum nsibs  birthorder, d

** R's Own Education **
tab1 educ 

** Parental Education **
tab1 faeduc maeduc 

** R's Own Occupation **
tab1 firstocc   
tab1 occ  

log close

** 11. Keep the identified variables only: done **

** 12. Save the Data File: skipped **

** 13. Harmonize education variables: referred to UNESCO mapping guide **

recode educ (0 17=.) (1=3) (2=6) (3/4=9) (5/6=12) (7=14) (8/9=15) (10=16) (11/15=17.5) (16=21.5),ge(educ_yrs)
lab var educ_yrs "respondent highest education in years"

recode educ (0 17=.) (1=0) (2=100) (3=254) (4=244) (5=354) (6=344) (7=550) (8/10=660) (11/15=760) (16=860),ge(educ_isced)
lab var educ_isced "respondent highest education in ISCED code"


recode faeduc maeduc (1=0) (2=3) (3=6) (4/5=9) (6/7=12) (8=14) (9/10=15) (11=16) (12/16=17.5) (17=21.5) (18=.),ge(faeduc_yrs maeduc_yrs)
lab var faeduc_yrs "father's education in years"
lab var maeduc_yrs "mother's education in years"

recode faeduc maeduc (1/2=0) (3=100) (4=254) (5=244) (6=354) (7=344) (8=550) (9/11=660) (12/16=760) (17=860) (18=.),ge(faeduc_isced maeduc_isced)
lab var faeduc_isced "father highest education in ISCED code"
lab var maeduc_isced "mother highest education in ISCED code"

//father's education is actually father's
ge faeduc_flag=1 
lab def faeduc_flag 1 "father" 2 "parent"
lab val faeduc_flag faeduc_flag

** 14. Harmonize sibling variables **
ge nsibs_flag=99
lab var nsibs_flag "cutoff of total number of siblings"  // no cutoff in the original variable 

** 16. Save the Data File **

saveold  /*insert you work directory here*/, replace

